// pages/directAddress/directAddress.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.changeSuper()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */

  onShareAppMessage: function (res) {
    return {
      title: '酒吧订位打折兰桂坊九眼桥少陵路',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },
  changeSuper: function () {
    var that = this;
    wx.request({
      url: app.globalData.allUrl + "/addressAction/getAllAddress",
      data: {
        userID: wx.getStorageSync('userid')
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        console.log(res.data)
        if (res.data) {
          that.setData({//如果有地址，则显示在页面上
            addresses: res.data,
          })
        }   
      }
    })
    this.setData({
      chooseAddress: false
    })
  },
  toDerictBuy:function(e){
    wx.setStorageSync('checkboxchooseaddress', e.target.dataset)
    console.log(wx.getStorageSync('checkboxchooseaddress'))
    wx.navigateBack({
      delta:1
    })
  },
   deleteAdd: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.allUrl + "/addressAction/deleteAddressByAddId",
      data: {
        addId: that.data.deletId
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        that.setData({
          addresses: ""
        })
        if (wx.getStorageSync('checkboxchooseaddress').id == that.data.deletId) {//同时把本地的地址删除
          wx.setStorageSync('checkboxchooseaddress', "")
        }
        setTimeout(function () {
          that.changeSuper()
        }, 500)
      }
    })
  },
  alertModel:function(e){
    var that=this;
    wx.showModal({
      title: '提示',
      content: '是否删除地址',
      success: function (res) {
        if (res.confirm) {
          that.setData({
            deletId: e.target.dataset.addid
          })
          that.deleteAdd()
        } else if (res.cancel) {
         
        }
      }
    })
  }
})